package sdk.org.apache.shiro.G2_使用.C07_1Web集成_登录和退出.AppConfig.config;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import sdk.org.apache.shiro.G2_使用.C07_1Web集成_登录和退出.shiro.MyShiroRealm;

@Configuration
public class ShiroConfiguration {
    //权限管理，配置主要是Realm的管理认证
    @Bean
    public org.apache.shiro.mgt.SecurityManager securityManager() {
        AuthorizingRealm realm = new MyShiroRealm();
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }
    
    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(org.apache.shiro.mgt.SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        shiroFilter.setLoginUrl("/login/page");   // 登录
        
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/login/**",      "anon");
        filterMap.put("/error",         "anon");
        filterMap.put("/",              "anon");
        filterMap.put("/favicon.ico",   "anon");
        // S0004_TitanAPI 通用接口平台
        filterMap.put("/titan/monitor/**",      "anon");
        filterMap.put("/titan/view/cache",      "anon");
        filterMap.put("/titan/api",             "anon");
        //登出
        filterMap.put("/logout",                "logout");
        //对所有用户认证
        filterMap.put("/**",                    "authc");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        return shiroFilter;
    }
    
    
    //加入注解的使用，不加入这个注解不生效
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(org.apache.shiro.mgt.SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
